// pages/teacher/kaoqin/index.js
import {
  kaoqin,
  stopkaoqin,
  index
} from "../../../api/kaoqin";
import {
  parseTime
} from "../../../utils/datetime.js";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    banjiId: String,
    banjiName:String
  },
  observers: {
    banjiId: function (value) {
      if (value == undefined || value == "") return
      this.BanjiRizhi()
      setTimeout(() => {
        this.loadData()
      }, 35000)
      // console.log(value)
    }
  },

  // 在组件实例进入页面节点树时执行
  lifetimes: {
    // attached: function () {
    //   // setInterval(function () {
    //   //   this.loadData()
    //   // }, 3000)
    //   setTimeout(() => {
    //     this.loadData()
    //   }, 15000)
    // }
  },

  // 页面被展示
  // pageLifetimes: {
  //   // show: function () {
  //   //   setInterval(function () {
  //   //     this.loadData()
  //   //   }, 3000)
  //   // },
  // },

  /**
   * 组件的初始数据
   */
  data: {
    arr:[],
    disabled: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    BanjiRizhi:function(){
      let data = {
        banjiid: this.data.banjiId
      }
      index(data).then(res=>{
        res.data.forEach(item => {
          item.create_time = parseTime(item.create_time, "{y}/{m}/{d} {h}:{i}:{s}")
          // console.log("timeeeeeeeeeeeee",item.create_time)
        })
        let arr = this.data.arr
        arr = arr.concat(res.data)
        console.log('qiandaojilu',arr)
        this.setData({
          arr:arr
        })
      })
    },
    //点击开始考勤
    onClick: function () {
      // this.start_attendence()
      let data = {
        banjiid: this.data.banjiId
      }
      kaoqin(data).then(res => {
        console.log("okkk5656", res)
        wx.showToast({
          title: res,
          icon: "none"
        })
        this.setData({
          disabled: true
        })
      })
    },

    //结束考勤
    loadData: function () {
      let data = {
        banjiid: this.data.banjiId
      }
      stopkaoqin(data).then(res => {
        console.log("okkk5656", res)
        wx.showToast({
          title: res,
          icon: "none"
        })
        this.setData({
          disabled: false
        })
      })
    },
    //已签到学生名单
    onIndex: function () {
      let data = {
        banjiid: this.data.banjiId,
        banjiname:this.data.banjiName
      }
      wx.navigateTo({
        url: '/pages/teacher/qiandao/index',
        success: res => {
          res.eventChannel.emit("send", data)
        },
      })
    },
    //未签到学生名单
    onWeiqian: function () {
      let data = {
        banjiid: this.data.banjiId,
        banjiname:this.data.banjiName
      }
      wx.navigateTo({
        url: '/pages/teacher/qiandao/weiqian',
        success: res => {
          res.eventChannel.emit("weisend", data)
        },
      })
    },
    //签到记录
    onDetail:function(e){
      let index = e.currentTarget.dataset.index
      let data = this.data.arr[index]
      console.log("qiandoa",data)
      wx.navigateTo({
        url: '/pages/teacher/kaoqin/detail',
        success: res => {
          res.eventChannel.emit("send", data)
        },
      })
    }
  }
})